﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Dapper;

namespace _180FrameWork.DataBaseHelper
{
    public static class DapperExtensions
    {
        /// <summary>
        /// 获取
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="param"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public static T Get<T>(this IDbConnection obj, object param, IDbTransaction transaction = null)
        {
            ISqlBuilder sqlBuilder = SqlBuilderFactory.CreateSqlBuilder(obj);
            return obj.Query<T>(sqlBuilder.GetSelectSQL<T>(), param, transaction).FirstOrDefault();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="data"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public static bool Insert<T>(this IDbConnection obj, object data, IDbTransaction transaction = null)
        {
            ISqlBuilder sqlBuilder = SqlBuilderFactory.CreateSqlBuilder(obj);
            int i = obj.Execute(sqlBuilder.GetInsertSQL<T>(), data, transaction);
            if (i > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="data"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public static bool Update<T>(this IDbConnection obj, object data, IDbTransaction transaction = null)
        {
            ISqlBuilder sqlBuilder = SqlBuilderFactory.CreateSqlBuilder(obj);
            int i = obj.Execute(sqlBuilder.GetUpdateSQL<T>(data), data, transaction);
            if (i > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="data"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public static bool Delete<T>(this IDbConnection obj, object data, IDbTransaction transaction = null)
        {
            ISqlBuilder sqlBuilder = SqlBuilderFactory.CreateSqlBuilder(obj);
            int i = obj.Execute(sqlBuilder.GetDeleteSQL<T>(), data, transaction);
            if (i > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        public static PageData<T> Page<T>(this IDbConnection obj, string sql, IDictionary<string, object> param, IDbTransaction transaction = null)
        {
            int page = 1; 
            int rows = 20; 
            string sort = ""; 
            string order = ""; 
            int total = 0; 
            string SUM = "";
            foreach (KeyValuePair<string, object> item in param)
            {
                switch (item.Key)
                {
                    case "page":
                        page = Convert.ToInt32(item.Value);
                        break;
                    case "rows":
                        rows = Convert.ToInt32(item.Value);
                        break;
                    case "sort":
                        sort = Convert.ToString(item.Value);
                        break;
                    case "order":
                        order = Convert.ToString(item.Value);
                        break;
                    //default:
                    //    dp.Add(item.Key, item.Value);
                    //    break;
                }
            }
     
            var p = new PageData<T>();
            if (total != 0)
            {
                p.total = total;
            }
            else
            {
                string strTotalSql = "select count(*) from(" + sql + ") t";
                p.total = obj.ExecuteScalar<int>(strTotalSql, param);
                if (p.total == 0)
                    p.total = 1;
            }
            p.totalPages = (p.total / rows) + (p.total % rows > 0 ? 1 : 0);
            //汇总
            if (SUM != "")
            {
                string strSumSql = "select " + SUM + " from(" + sql + ") t";
                p.footer = obj.Query<T>(strSumSql, param).ToList();
            }
            //排序
            string orderby = "";
            if (sort != "")
            {
                orderby = " order by " + sort;
                if (order != "")
                {
                    orderby += " " + order;
                }
            }
            //获取分页数据
            int beginrow = rows * (page - 1) + 1;
            int endrow = rows * page;
            param.Add("beginrow", beginrow);
            param.Add("endrow", endrow);
            string strDataSql = "select c.* from(select t.*,ROWNUM AS RN from(select x.* from(" + sql + ")x " + orderby + " )t where rownum<=:ENDROW)c WHERE C.RN>=:BEGINROW ";
            p.rows = obj.Query<T>(strDataSql, param).ToList();
            return p;
        }
    }
}
